#coding:utf-8
# 矩阵转置

from Matrix import Matrix

from _det import _det
from _trans import _trans

def calcDet(M,i,j):
    sign = (-1) ** (i+j)
    rM = [ 
        [
           M[x][y] for y in range(0,M.col) if y != j   
        ] for x in range(0,M.row) if x != i 
    ]

    return sign * _det(
        Matrix(*rM)
    )

def _adj(this):
    return _trans(
        Matrix(*[
            [
                calcDet(this,i,j) for j in xrange(0,this.col)
            ] for i in xrange(0,this.row)
        ])
    )

# 测试语句
def test():
    mt = Matrix(
        [-3,2,-5],
        [-1,0,-2],
        [3,-4,1])
    
    print(_adj(mt))
    print('正确答案应该是:')
    print( Matrix(
        [-8,18,-4],
        [-5,12,-1],
        [4,-6,2])
    )

    
if __name__=='__main__':
    import sys
    test()
